package cn.gdk.gaodekecc.service.impl;

import cn.gdk.gaodekecc.domain.MoveUser;
import cn.gdk.gaodekecc.domain.MovieTicket;
import cn.gdk.gaodekecc.domain.User;
import cn.gdk.gaodekecc.exception.GlobalException;
import cn.gdk.gaodekecc.exception.GlobalExceptionCodeEnum;
import cn.gdk.gaodekecc.feign.MoveTicketService;
import cn.gdk.gaodekecc.mapper.UserMapper;
import cn.gdk.gaodekecc.result.JSONResult;
import cn.gdk.gaodekecc.service.IUserService;
import cn.gdk.gaodekecc.util.AssertUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-11-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private MoveTicketService moveTicketService;


    /**
     * 查询电影票的接口
     * @return
     */
    @Override
    public JSONResult queryMovie() {
        return JSONResult.success(moveTicketService.list());
    }

    /**
     * 电影票购买接口
     * @param movieId 电影票id
     * @param count 购买的数量
     * @return
     */
    @Override
    public JSONResult buy(Long movieId, Integer count) {
        // 根据电影票的id获取到电影票判断电影票是否存在不存在抛异常
        JSONResult jsonResult = moveTicketService.get(movieId);
        // 报错就返回电影服务的错误信息
        if(!jsonResult.isSuccess()){
            return jsonResult;
        }
        // 判断电影票是否存在
        Object obj = jsonResult.getData();
        if(obj==null){
            throw  new GlobalException("电影票已下架");
        }
        // 判断电影票的余票是否满足我们购买的需求
        MovieTicket movieTicket = JSONObject.parseObject(JSONObject.toJSONString(obj), MovieTicket.class);
        if(movieTicket.getStock()==null ||movieTicket.getStock()< count){
            throw  new GlobalException("电影票库存不够");
        }
        // 查询用户余额是否满足购买需求目前用户写死
        User user = selectById(1L);
        BigDecimal multiply = movieTicket.getPrice().multiply(new BigDecimal(count));
        if(user.getPrice().compareTo(multiply)<0){
            throw  new GlobalException("余额不足");
        }
        // 判断电影票时间
        Date date = new Date();
        if(!date.before(movieTicket.getEndTime())){
            throw  new GlobalException("电影票已过期");
        }
        // 购买电影
        JSONResult buy = moveTicketService.buymovie(movieId, count);
        MovieTicket movieTicket1 = JSONObject.parseObject(JSONObject.toJSONString(buy.getData()), MovieTicket.class);
        if(!buy.isSuccess()){
            return buy ;
        }
        user.setPrice(user.getPrice().subtract(multiply));
        updateById(user);
        return jsonResult.success("出票成功");
    }

    /**
     * 电影票观看接口
     * @param movieId  电影票id
     * @param count  观看数量
     * @param userId  用户的id
     * @return
     */
    @Override
    public JSONResult movieTicketViewing(Long movieId, Integer count, Long userId) {
        // 查询电影票
        JSONResult jsonResult = moveTicketService.get(movieId);
        if(!jsonResult.isSuccess()){
            return jsonResult;
        }
        if(jsonResult.getData()==null){
            throw  new GlobalException("电影票不存在");
        }
        MovieTicket movieTicket = JSONObject.parseObject(JSONObject.toJSONString(jsonResult.getData()), MovieTicket.class);
        // 查询该用户的票数
        JSONResult jsonResult1 = moveTicketService.movieTicketViewing(movieId, userId);
        if(!jsonResult1.isSuccess()){
            return jsonResult1;
        }
        MoveUser moveUser = JSONObject.parseObject(JSONObject.toJSONString(jsonResult1.getData()), MoveUser.class);
        // 判断看的票数是否够
        if(moveUser.getCount()<count){
            throw  new GlobalException("电影票不够观看");
        }
        Date date = new Date();
        if(!date.before(movieTicket.getEndTime())){
            throw  new GlobalException("电影票过期");
        }
        moveUser.setCount(moveUser.getCount()-count);
        moveTicketService.saveOrUpdate(moveUser);
        return jsonResult.success("电影观看成功");
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public JSONResult register(User user) {
        JSONResult list = moveTicketService.list();
        if(!list.isSuccess()){
            return list;
        }
        List<MovieTicket> movieTicket = JSONObject.parseArray(JSONObject.toJSONString(list.getData()), MovieTicket.class);
        Random random = new Random();
        List<MovieTicket> collect = movieTicket.stream().filter(MovieTicket -> MovieTicket.getStock() >= 1).collect(Collectors.toList());
        MovieTicket movieTicket1 = collect.get(random.nextInt(collect.size()));
        AssertUtil.isNotNull(movieTicket1, GlobalExceptionCodeEnum.ERROR);
        insert(user);
        JSONResult buymovie = moveTicketService.buymovie(movieTicket1.getId(), 1);
        if(!buymovie.isSuccess()){
            return buymovie;
        }
        return JSONResult.success();
    }
}
